package com.ruoyi.common.handler;

import com.ruoyi.common.enums.BaseInteger;
import com.ruoyi.common.enums.BaseString;
import com.ruoyi.common.utils.reflect.ReflectUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class EnumTypeHandler <E extends Enum<E>> extends BaseTypeHandler<E> {

    private final Class<E> type;

    public EnumTypeHandler(Class<E> type) {
        if (type == null) {
            throw new IllegalArgumentException("Type argument cannot be null");
        }
        this.type = type;
    }

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {
        if (ReflectUtils.isSuperInterface(type, BaseInteger.class)) {
            BaseIntegerEnumTypeHandler baseIntegerEnumTypeHandler = new BaseIntegerEnumTypeHandler(type);
            baseIntegerEnumTypeHandler.setNonNullParameter(ps,i, (BaseInteger) parameter,jdbcType);

        }else if (ReflectUtils.isSuperInterface(type, BaseString.class)) {
            BaseStringEnumTypeHandler baseStringEnumTypeHandler =  new BaseStringEnumTypeHandler(type);
            baseStringEnumTypeHandler.setNonNullParameter(ps,i, (BaseString) parameter,jdbcType);
        }

        if (jdbcType == null) {
            ps.setString(i, parameter.name());
        } else {
            ps.setObject(i, parameter.name(), jdbcType.TYPE_CODE); // see r3589
        }
    }

    @Override
    public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
        if (ReflectUtils.isSuperInterface(type, BaseInteger.class)) {
            BaseIntegerEnumTypeHandler baseIntegerEnumTypeHandler = new BaseIntegerEnumTypeHandler(type);
            return (E)baseIntegerEnumTypeHandler.getNullableResult(rs, columnName);
        }else if (ReflectUtils.isSuperInterface(type, BaseString.class)) {
            BaseStringEnumTypeHandler baseStringEnumTypeHandler =  new BaseStringEnumTypeHandler(type);
            return (E)baseStringEnumTypeHandler.getNullableResult(rs, columnName);
        }
        String s = rs.getString(columnName);
        return s == null ? null : Enum.valueOf(type, s);
    }

    @Override
    public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        if (ReflectUtils.isSuperInterface(type, BaseInteger.class)) {
            BaseIntegerEnumTypeHandler baseIntegerEnumTypeHandler = new BaseIntegerEnumTypeHandler(type);
            return (E)baseIntegerEnumTypeHandler.getNullableResult(rs, columnIndex);
        }else if (ReflectUtils.isSuperInterface(type, BaseString.class)) {
            BaseStringEnumTypeHandler baseStringEnumTypeHandler =  new BaseStringEnumTypeHandler(type);
            return (E)baseStringEnumTypeHandler.getNullableResult(rs, columnIndex);
        }
        String s = rs.getString(columnIndex);
        return s == null ? null : Enum.valueOf(type, s);
    }

    @Override
    public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        if (ReflectUtils.isSuperInterface(type, BaseInteger.class)) {
            BaseIntegerEnumTypeHandler baseIntegerEnumTypeHandler = new BaseIntegerEnumTypeHandler(type);
            return (E)baseIntegerEnumTypeHandler.getNullableResult(cs, columnIndex);
        }else if (ReflectUtils.isSuperInterface(type, BaseString.class)) {
            BaseStringEnumTypeHandler baseStringEnumTypeHandler =  new BaseStringEnumTypeHandler(type);
            return (E)baseStringEnumTypeHandler.getNullableResult(cs, columnIndex);
        }
        String s = cs.getString(columnIndex);
        return s == null ? null : Enum.valueOf(type, s);
    }
}